package com.lsmw.commonutils;
/*
 *ClassName:MD5
 *UserName:二号男嘉宾
 *CreateTime:2020/12/17 17:12
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public final class MD5 {
    public static final String SECRET = "XX_IS_SHIT";
    public static String encrypt(String strSrc) {
        try {
            // 用来将字节转换成 16 进制表示的字符
            char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            byte[] bytes = (strSrc+SECRET).getBytes();
            //获取MD5信息摘要处理的对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            //传入需处理byte数据
            md.update(bytes);
            //完成加密hash计算并返回结果,产生128 位的长整数
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            // 从第一个字节开始，对每一个字节,转换成 16 进制字符(取每8位转为两个16进制数)
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                //取高4位进行转换
                chars[k++] = hexChars[b >>> 4 & 0xf];
                //取低4位进行转换
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }


}

